﻿/**
 * index
 * 20180821-6
 * @author yangyibao
 * @version 1.0.0
 */

var rateDataTable;
$(function(){
	var date = [];
	var g_cpuData = [];
	var totalPoints = 250;
	
	function initMenu(){
		$.post("../user/roleList",{userId:$("#input_sysUserId").val()},function(data){
			var role = data.role;
			$.post("../power/queryPermissionListIndex",{roleId:role,userId:$("#input_sysUserId").val()},function (data){
				for(var i=0; i<data.length;i++){
					var tmp = data[i];
					var menu = tmp.permissionExt1;
					$("#tiles_sys " +"#tiles_" + menu).css("display","block");
				}
				if(role == "6"){
					$("#tiles_organMgr_div").removeClass("double-down");
				}
			});
		});
		
    }
    initMenu();
	//初始化cpu统计图表
	function initCpuCharts() {
        if (!jQuery.plot) {
            return;
        }
        //获取cpu数据
        function getCPUData(val) {
            if (g_cpuData.length > 0) g_cpuData = g_cpuData.slice(1);
            if(val){
            	g_cpuData.push(val);
            }else{
            	while (g_cpuData.length < totalPoints) {
            		g_cpuData.push(0);
              }
            }
            var res = [];
            for (var i = 0; i < g_cpuData.length; ++i) res.push([i, g_cpuData[i]])
            return res;
        }
        //初始化cpu使用率
        if ($('#load_statistics').size() != 0) {
            //server load
           $('#load_statistics_loading').hide();
           $('#load_statistics_content').show();
           var updateInterval = 30;
           var plot_statistics = $.plot($("#load_statistics"), [getCPUData()], {
           series: {
               shadowSize: 1
           },
           lines: {
               show: true,
               lineWidth: 0.2,
               fill: true,
               fillColor: {
                   colors: [{
                           opacity: 0.1
                       }, {
                           opacity: 1
                       }
                   ]
               }
           },
           yaxis: {
               min: 0,
               max: 100,
               tickFormatter: function (v) {
                   return v + "%";
               }
           },
           xaxis: {
               show: false
           },
           colors: ["#e14e3d"],
           grid: {
               tickColor: "#a8a3a3",
               borderWidth: 0
           }
           });
           function statisticsUpdate() {
//        	   $.get("../system/sysJvmMonitor",function(value){
//        		   plot_statistics.setData([getCPUData(value)]);
//            	   plot_statistics.draw();
//        	   }); 
//        	   setTimeout(statisticsUpdate, updateInterval);
           }
           statisticsUpdate();
           $('#load_statistics').bind("mouseleave", function () {
               $("#tooltip").remove();
           });
       }

    }
	
	
	function randValue() {
         return (Math.floor(Math.random() * (1 + 50 - 20))) + 10;
    }
	
	var days = 30;
	function getCurDate(){
		var curDate = new Date(); 
		var year = curDate.getYear();  
		var month = curDate.getMonth()+1;
		if(month<10){
			month = "0"+month;
		}
		var day = curDate.getDate(); 
		return year+'-'+month+"-"+day;
	}
	
	//初始化服务器基本信息
	function initMiniCharts() {
        $('.easy-pie-chart .number.transactions').easyPieChart({
            animate: 1000,
            size: 75,
            lineWidth: 3,
            barColor: App.getLayoutColorCode('yellow')
        });

        $('.easy-pie-chart .number.visits').easyPieChart({
            animate: 1000,
            size: 75,
            lineWidth: 3,
            barColor: App.getLayoutColorCode('green')
        });
         
        $('.easy-pie-chart .number.bounce').easyPieChart({
            animate: 1000,
            size: 75,
            lineWidth: 3,
            barColor: App.getLayoutColorCode('red')
        });
        
      //封装cpu的方法
        function cpu(){
//        	$.get("/mcode/system/sysCpuMonitor",function(data){
//        		$('.easy-pie-chart .number.transactions').data('easyPieChart').update(data);
//        		$('span', $('.easy-pie-chart .number.transactions')).text(data);
//        	});
        }
        //封装mem内存的方法
        function mem(){
//        	$.get("/mcode/system/sysMemMonitor",function(data){
//        		$('.easy-pie-chart .number.visits').data('easyPieChart').update(data);
//        		$('span', $('.easy-pie-chart .number.visits')).text(data);
//        	});
        }
        
        function disk(){
//        	$.get("/mcode/system/sysDiskUsed",function(data){
//        		$('.easy-pie-chart .number.bounce').data('easyPieChart').update(data);
//        		$('span', $('.easy-pie-chart .number.bounce')).text(data);
//        	});
        }
        
        $('.easy-pie-chart-reload').click(function(){
            cpu();
            mem();
            disk()
        });
           
        cpu();
        mem();
        disk();
        
        $("#sparkline_bar").sparkline([8,9,10,11,10,10,12,10,10,11,9,12,11,10,9,11,13,13,12], {
            type: 'bar',
            width: '100',
            barWidth: 5,
            height: '55',
            barColor: '#35aa47',
            negBarColor: '#e02222'}
        );

        $("#sparkline_bar2").sparkline([9,11,12,13,12,13,10,14,13,11,11,12,11,11,10,12,11,10], {
            type: 'bar',
            width: '100',
            barWidth: 5,
            height: '55',
            barColor: '#ffb848',
            negBarColor: '#e02222'}
        );

        $("#sparkline_line").sparkline([9,10,9,10,10,11,12,10,10,11,11,12,11,10,12,11,10,12], {
            type: 'line',
            width: '100',
            height: '55',
            lineColor: '#ffb848'
        });
    }
	
	//初始化cpu统计
	initCpuCharts();
	//初始化基本信息
	initMiniCharts();
	$('#site_statistics_loading').hide();
    $('#site_statistics_content').show();
    
    
    

	
	

	var handlePulsate = function () {
        if (!jQuery().pulsate) {
            return;
        }
        if (App.isIE8() == true) {
            return; // pulsate plugin does not support IE8 and below
        }
        if (jQuery().pulsate) {
            jQuery('#pulsate-regular').pulsate({
                color: "#bf1c56"
            });
            jQuery('#pulsate-once').click(function () {
                $(this).pulsate({
                    color: "#399bc3",
                    repeat: false
                });
            });
            jQuery('#pulsate-hover').pulsate({
                color: "#5ebf5e",
                repeat: false,
                onHover: true
            });
            jQuery('#pulsate-crazy').click(function () {
                $(this).pulsate({
                    color: "#fdbe41",
                    reach: 50,
                    repeat: 10,
                    speed: 100,
                    glow: true
                });
            });
        }
    }
    var handleGritterNotifications = function () {
        if (!jQuery.gritter) {
            return;
        }
        $('#gritter-sticky').click(function () {
            var unique_id = $.gritter.add({
                title: 'This is a sticky notice!',
                text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus eget tincidunt velit. Cum sociis natoque penatibus et <a href="#">some link sample</a> montes, nascetur ridiculus mus.',
                image: './assets/img/avatar1.jpg',
                sticky: true,
                time: '',
                class_name: 'my-sticky-class'
            });
            return false;
        });

        $('#gritter-regular').click(function () {
            $.gritter.add({
                title: 'This is a regular notice!',
                text: 'This will fade out after a certain amount of time. Vivamus eget tincidunt velit. Cum sociis natoque penatibus et <a href="#">some link sample</a> montes, nascetur ridiculus mus.',
                image: './assets/img/avatar1.jpg',
                sticky: false,
                time: ''
            });
            return false;
        });

        $('#gritter-max').click(function () {
            $.gritter.add({
                title: 'This is a notice with a max of 3 on screen at one time!',
                text: 'This will fade out after a certain amount of time. Vivamus eget tincidunt velit. Cum sociis natoque penatibus et <a href="#">some link sample</a> montes, nascetur ridiculus mus.',
                image: './assets/img/avatar1.jpg',
                sticky: false,
                before_open: function () {
                    if ($('.gritter-item-wrapper').length == 3) {
                        return false;
                    }
                }
            });
            return false;
        });

        $('#gritter-without-image').click(function () {
            $.gritter.add({
                title: 'This is a notice without an image!',
                text: 'This will fade out after a certain amount of time. Vivamus eget tincidunt velit. Cum sociis natoque penatibus et <a href="#">some link sample</a> montes, nascetur ridiculus mus.'
            });
            return false;
        });

        $('#gritter-light').click(function () {
            $.gritter.add({
                title: 'This is a light notification',
                text: 'Just add a "gritter-light" class_name to your $.gritter.add or globally to $.gritter.options.class_name',
                class_name: 'gritter-light'
            });
            return false;
        });

        $("#gritter-remove-all").click(function () {
            $.gritter.removeAll();
            return false;

        });
    }
    
    function getPageHtml(data){
    	var str = "";
    	for(var i=0; i< data.length;i++){
			var tmp = data[i];
			var name = tmp.helpName;
			var inTime = tmp.helpIntime.substring(0,7);
			var subStr = "<li>" +
					"<div class='col1'>" +
						"<div class='cont'>" +
							"<div class='cont-col1'>" +
								"<div class='label label-success'>" +
									"<i class='icon-bullhorn'></i>" +
								"</div>" +
							"</div>" +
							"<div class='cont-col2'>" +
								"<div class='desc'><a href=/mcode/resources/download?fileName="+ tmp.helpPath +">"+name+ "</a></div>" +
							"</div>" +
						"</div>" +
					"</div>" +
					"<div class='col2'>" +
						"<div class='date'>"+inTime+"</div>" +
					"</div>" +
				"</li>";
			str = str + subStr;
		}
    	return str;
    }

   function handleDynamicPagination(total) {
        $('#dynamic_pager_demo1').bootpag({
            total: total,
            page: 1,
        }).on("page", function(event, num){
        	$.ajax({
        		url: "../system/helpTestVO/queryHelpTestVOPageList?iDisplayStart="+(num-1)*10+"&iDisplayLength=10"
    		}).done(function(data) {
    			$("#dynamic_pager_content1").html(getPageHtml(data.aaData));
    		});
    		
        });
    }
    
    function initHelpTabel(){
    	handlePulsate();
	    handleGritterNotifications();
    	$.ajax({
    		url: "../system/helpTestVO/queryHelpTestVOPageList?iDisplayStart="+0+"&iDisplayLength=10"
		}).done(function(data) {
			var iTotalRecords= data.iTotalRecords;
			var num = iTotalRecords%10;
			
			if(num == 0){
				handleDynamicPagination(iTotalRecords/10);
			}else{
				handleDynamicPagination(iTotalRecords/10 + 1);
			}
			$("#dynamic_pager_content1").html(getPageHtml(data.aaData));
			
		   // handleDynamicPagination();
		});
    }
    initHelpTabel();
});
//培训机构开班培训科目统计饼状图
function numGraph(years){}
//科目报名人数柱状图
function classGraph(years){}
//机构通过率统计图
function passingGraph(passId){}

function reloadrateDataTable(){
	rateDataTable.ajax.reload();
}